package com.eblacorp.archive.util;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class HttpUtil {
	
	final static Log log = LogFactory.getLog(HttpUtil.class);
	
	public static String sendHttpRequest(String params, String endPoint) {
		try {
            // Send the request
            URL url = new URL(endPoint);
            URLConnection urlConnection = url.openConnection();
            urlConnection.setDoOutput(true);
            OutputStreamWriter writer = new OutputStreamWriter(urlConnection.getOutputStream());
            
            //write parameters
            writer.write(params);
            writer.flush();
            
            // Get the response
            StringBuffer response = new StringBuffer();
            BufferedReader reader = new BufferedReader(new InputStreamReader(urlConnection.getInputStream()));
            String line;
            while ((line = reader.readLine()) != null) {
            	response.append(line);
            }
            writer.close();
            reader.close();
            
            //Output the response
            return response.toString();
            
        } catch (MalformedURLException ex) {
        	log.error(ex);
        } catch (IOException ex) {
        	log.error(ex);
        }
        return null;
	}
}
